﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using miSuperEngine;
namespace juego1
{
    class inGameScene:Scene
    {
        List<Actor> enemies = new List<Actor>();
        List<Actor3D> naves= new List<Actor3D>();
        Actor player;
        Random rand=new Random();
        Model shipMod;
        PlayerIndex playingPlayer;
        //Vector3 cameraPosition = new Vector3(3000, 0, 0);
        Actor3D actorcito;
        Camera gameCam=new Camera(new Vector3(3000, 0, 0),new Vector3(0,0,0),Vector3.Up);
        public inGameScene(ContentManager c)
            : base(c)
        {
            Texture2D enemigoTex=c.Load<Texture2D>("spritesheet");
            for (int i=0;i<30;i++)
                enemies.Add(new Enemigo(new Vector2(rand.Next(0, (int)SceneManager.screenSize.X), rand.Next(0, (int)SceneManager.screenSize.Y)), new Animation(enemigoTex, 8, 8, 60, 30)));
            player = new Player(new Vector2(SceneManager.screenSize.X / 4, SceneManager.screenSize.Y / 2), new Animation(c.Load<Texture2D>("vaca"), 1, 1, 1, 1));
            //shipModel = c.Load<Model>("Ship");
            actorcito=new Actor3D(Vector3.Zero,Vector3.Zero,Vector3.Zero,c.Load<Model>("ship"));
            shipMod = c.Load<Model>("ship");
        }
        public override void draw(Microsoft.Xna.Framework.Graphics.SpriteBatch sb)
        {
            sb.Begin(SpriteBlendMode.AlphaBlend,SpriteSortMode.FrontToBack,SaveStateMode.SaveState);
            foreach (Actor act in enemies)
            {
                act.draw(sb);
            }
            player.draw(sb);
            sb.End();
            actorcito.draw(gameCam);

            foreach (Actor3D act in naves)
            {
                if (act.isActive)
                act.draw(gameCam);
            }

            
            base.draw(sb);
        }
        public void createNewShip(Vector3 pos, Vector3 spe, Vector3 accel, Model mod)
        {
            foreach (Actor3D nave in naves)
            {
                if (!nave.isActive)
                {
                    nave.newActor3D(pos, spe, accel, mod);
                    return;
                }
            }
            naves.Add(new Actor3D( pos,  spe,  accel,  mod));
        }
        public override void handleInput()
        {
            
                /*if (Keyboard.GetState().IsKeyDown(Keys.Space))
                actors.Add(new Actor(new Vector2(50, 50), vacaTex));*/
            PlayerIndex pi;
                if (SceneManager.input.isNewButtonDown(Buttons.X, null, out pi)/*SceneManager.input.isNewKeyPress(Keys.Space)*//*Keyboard.GetState().IsKeyDown(Keys.Space)*/)
                {
                    naves.Add(new Actor3D(gameCam.getPosition(), gameCam.getDirection() * 100, Vector3.Zero, shipMod));
                    System.Console.WriteLine(pi);
                }
            if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                gameCam.moveForward(100);
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Down))
            {
                gameCam.moveForward(-100);
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                gameCam.rotateY(MathHelper.ToRadians(1));
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                gameCam.rotateY(MathHelper.ToRadians(-1));
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Z))
            {
                gameCam.strafe(-100);
            }
            if (Keyboard.GetState().IsKeyDown(Keys.X))
            {
                gameCam.strafe(100);
            }
            base.handleInput();
        }
        public override void update(Microsoft.Xna.Framework.GameTime gt)
        {
            actorcito.update(gt);
            foreach (Actor act in enemies)
            {
                act.update(gt);
                if (act.boundingBox.Intersects(player.boundingBox))
                {
                    System.Console.WriteLine("collision!!!!");
                }
            }
            foreach (Actor3D act in naves)
            {
                if (act.isActive)
                    act.update(gt);
            }




            player.update(gt);
            base.update(gt);
        }
    }
}
